package com.newtribe.access;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class Packet  {
	public static void main(String[] sa){
		Packet p =new Packet(22) ;
		System.out.println(p.capacity());
		p.write32(32) ;
		p.write16(16);
		p.write8(8) ;
		p.write32(99);
		p.markEnd();
		p.flip();
		byte[] array = new byte[p.getSize()];
		p.copyToByteArray(array, 0, p.getSize());
		int j1 =p.read32();
		int j2 =p.read16();
		int j3 =p.read8();
		System.out.println(p.read32());
		
	}
	
	private ByteBuffer bb ;
	DataInputStream din ;
	DataOutputStream dout ;
	
	public Packet() {
		bb =ByteBuffer.allocate(128) ;
		
	}
	
	public Packet(int length) {
		bb =ByteBuffer.allocate(length) ;
	}		


	
	public int read8() {
		 return bb.get()&0xff;
	}

	
	public int read16(){
		// TODO Auto-generated method stub
		return bb.getShort();
	}

	
	public int read32() {
		// TODO Auto-generated method stub
		return bb.getInt();
	}

	
	public String readString(int len)  {
		// TODO Auto-generated method stub
		byte [] buff =new byte[len] ;
		bb.get(buff,0,len) ;
		
		return new String(buff) ;
	}


	
	public void skipBytes(int n)  {
		// TODO Auto-generated method stub
		  bb.position(bb.position()+n);
	}

	
	public void write(byte[] b, int off, int len)  {
		bb.put(b, off, len) ;
		
		
	}

	
	public void write(byte[] b)  {
		bb.put(b) ;
		
	}

	
	public void write32(int b)  {
	    bb.putInt(b);
		
	}

	
	public void write8(int v)  {
		// TODO Auto-generated method stub
		bb.put((byte)(v&0xff));
		
	}


	
	public void write16(int v)  {
		// TODO Auto-generated method stub
		bb.putShort((short)v);
	}
	
	   public int capacity(){
	       return bb.capacity();
	   }
	   
	   
		public int getPosition() {
		return bb.position();
		}
		

		public void setPosition(int position) {
		bb.position(position);
		}
		int end =-1 ;
		public void markEnd() {
			this.end =this.getPosition();
		}
		public int getSize() {
			return this.end;
			
		}
		public void flip(){
			bb.flip();
		}

	public void copyToByteArray(byte[] array ,int array_offset,int len){
		
		int oldpos = getPosition();
		bb.position(0);
		bb.get(array,array_offset,len);
		setPosition(oldpos);
	}


}
